package com.zp.common.redis.CustomRedissonLockExecutor;

import com.baomidou.lock.LockInfo;
import com.baomidou.lock.LockTemplate;

/**
 * @program: zp-backstage
 * @ClassName LockServiceImpl
 * @description: 自定义测试类
 * @author: zhangpeng
 * @create: 2023-10-28 15:33
 * @Version 1.0
 **/
public class LockServiceImpl {


    private LockTemplate lockTemplate;

    public void lock (String resourceKey) {

        LockInfo lock = lockTemplate.lock(resourceKey, 10000L, 2000L, CustomRedissonLockExecutor.class);
        if (lock == null) {
            // 获取不到锁
            throw new RuntimeException("业务处理中，请稍后再试...");
        }
        // 获取锁成功，处理业务
        try {
            doBusiness();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            lockTemplate.releaseLock(lock);
        }
    }

    private void doBusiness () {
        // TODO 业务执行逻辑
    }
}
